//导入任务号码
var Base64 = {
  // base64的组成元素
  _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  // 对外暴露的加密方法
  encode: function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;
    input = Base64._utf8_encode(input);
    while (i < input.length) {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
        enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
        enc4 = 64;
      }

      output =
        output +
        this._keyStr.charAt(enc1) +
        this._keyStr.charAt(enc2) +
        this._keyStr.charAt(enc3) +
        this._keyStr.charAt(enc4);
    }

    return output;
  },

  // 对外暴露的解密方法
  decode: function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    while (i < input.length) {
      enc1 = this._keyStr.indexOf(input.charAt(i++));
      enc2 = this._keyStr.indexOf(input.charAt(i++));
      enc3 = this._keyStr.indexOf(input.charAt(i++));
      enc4 = this._keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }
    }

    output = Base64._utf8_decode(output);

    return output;
  },

  //  UTF-8格式加密
  _utf8_encode: function (string) {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {
      var c = string.charCodeAt(n);

      if (c < 128) {
        utftext += String.fromCharCode(c);
      } else if (c > 127 && c < 2048) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      } else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }

    return utftext;
  },

  // UTF-8格式解密
  _utf8_decode: function (utftext) {
    var string = "";
    var i = 0;
    var c = (c1 = c2 = 0);

    while (i < utftext.length) {
      c = utftext.charCodeAt(i);

      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      } else if (c > 191 && c < 224) {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = utftext.charCodeAt(i + 1);
        c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(
          ((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)
        );
        i += 3;
      }
    }
    return string;
  }
};
var importPhone = function () {
  var sqlParams = new Object();
  var params = {
    content: Params.content
  };
  sqlParams.uuid = Params.uuid;
  var sql1 =
    "select a.uuid,b.*,c.name,c.permission from app_open_api_c002_callout_user_verify_2 a left join app_open_api_c003_hbdh_user_1 b on a.user_id=b.id  left join app_open_api_c001_hbdh_role_1 c on b.role_id=c.id where a.uuid=#{uuid}";
  var result1 = JSON.parse(CustomizeUtil.abilitySql(sql1, sqlParams));
  if (result1.data.count > 0) {
    sqlParams.username = result1.data.results[0].username;
    var sql_log =
      "insert into app_open_api_c001_zhyd_log_6(phone,create_time,method) values(#{username},now(),'importPhone')";
    CustomizeUtil.abilitySql(sql_log, sqlParams);
    var destr = Base64.decode(Params.content);
    var array1 = destr.split("\n");
    var array2 = new Array();
    for (i = 0; i < array1.length; i++) {
      array2.push(array1[i].split("|"));
    }
    var creatresult;
    var repetArr = "";
    for (i = 0; i < array2.length; i++) {
      var tell = array2[i][0];
      if (tell !== "") {
        //检查是否已经被其他号码导入
        sqlParams.tell = array2[i][0];
        var sql =
          "select * from app_open_api_c003_hbdh_number_1 where tell=#{tell}";
        var result = JSON.parse(CustomizeUtil.abilitySql(sql, sqlParams));
        if (result.data.count > 0) {
          repetArr += array2[i][0] + '，';
          // 结果有重复，则不新增
        } else {

          sqlParams.handler_man = array2[i][1];
          sqlParams.chnl = array2[i][2];
          sql =
            "insert into app_open_api_c003_hbdh_number_1(tell,handler_man,chnl,is_valid,create_time,creator,res,is_freeze,is_convert) values(#{tell},#{handler_man},#{chnl},'否',now(),#{username},'','','')";
          creatresult = CustomizeUtil.abilitySql(sql, sqlParams);
        }
      }
    }
    if (repetArr.length > 0) {
      var repetResult =
        "{'data':{'results':'存在重复导入号码','tell': '" +
        repetArr +
        "'},'publicData':{'retInfo':{'retType':1}}}";
      JsResult.result = repetResult;
    } else {
      JsResult.result = creatresult;
    }
  } else {
    var result2 =
      "{'data':{'count':1,'results':'登录失效','status':1},'publicData':{'retInfo':{'retType':1,'retCode':0,'retMsg':'登录失效，请先登录'}}}";
    JsResult.result = result2;
  }
};
importPhone();
